/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.model.RoleModel;
import com.cxhd.lis.model.newmodel.LisUser;
import static com.cxhd.lis.model.newmodel.LisUser.lisUserdao;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cxhd
 */
@ControllerBind(controllerKey = "/view/lis/userList")
public class LisUserCtrl extends Controller {

    Gson gson = new Gson();
    private RoleModel role = new RoleModel();

    public void index() {
        render("/view/lis/userManager_1.jsp");
    }

    public void getUserList() {
        renderJson(Db.use("mysql").find("select a.*,b.description from lis_user a left join lis_system_role b on a.position=b.name"));
    }

    @Before(Tx.class)
    public void deleteUser() {

        try {
            Db.use("mysql").update("delete from lis_user where pk=" + Integer.valueOf(getPara("pk")));
//             Db.use("mysql").update("delete from lis_user where pk='2'");
//         new LisUser().deleteUser(Integer.parseInt(pk));
        } catch (Exception e) {
            throw e;
        }

        renderText("true");
    }

    public void editUser() {
//          Map<String, String> userInfo = gson.fromJson(getPara("user"), new TypeToken<Map<String, String>>() {}.getType());
        new LisUser().updateUser(this);
        renderText("true");
    }

    public void addUser() {
        new LisUser().addUser(getPara("user_id"), getPara("name"),
                getPara("birth"), getPara("address"), getPara("phone"), getPara("sex"), getPara("department"),
                getPara("position"), getPara("data1"), getPara("data2"), getPara("data3"), getPara("password"));
        renderText("true");
    }

    public void getLisUserReP() {
        List<Record> list = Db.use("mysql").find("select * from lis_user where name=?", getPara("name"));
        if (list.size() != 0) {

            if (getPara("pk") != "" && getPara("pk") != null) {//判断是新增还是修改
                if (Integer.valueOf(getPara("pk")) == list.get(0).get("pk")) { //修改是允许修改（本身主键和查询到的主键相同）
                    renderText("false");
                } else {
                    renderText("true");
                }
            } else {
                renderText("true");
            }
        } else {
            renderText("false");
        }

    }

    public void checkUserId() {
        renderText(new LisUser().checkId(getPara("userId")) + "");
    }

    public void getRole() {
        renderJson(role.getRole());
    }

    public void editpw() {
        new LisUser().editPW(getPara("user_id"), getPara("pw"), getPara("pk"));
        renderText("true");
    }
}
